﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FrmHotel
{
    public partial class FrmTianJiaXF : Form
    {
        public FrmTianJiaXF()
        {
            InitializeComponent();
        }
        public int id;//接收主菜单的房间号
        private void FrmTianJiaXF_Load(object sender, EventArgs e)
        {
            BoinComdoBox();
            BoinDataGridView1();
            this.label4.Text = id.ToString();

            dataGridView2.AutoGenerateColumns = false;
            string sql = string.Format("select * from Consume c,Commodity y,CetaeInfo o where o.CeId=c.CsoName and y.CyId=c.SsoName and c.CsoRoom=" + id);
            DataTable dt = DBHelper.SelectForTable(sql);
            dataGridView2.DataSource = dt;
            jiagezong();
        }
        public string zong;
        public void jiagezong()
        {
           string jisuan = "select Sum(CsPrice) from  Consume   where CsoRoom=" + id;
            zong  =DBHelper.SelectOne(jisuan).ToString();
            if (string.IsNullOrEmpty(zong))
            {
                zong = "0.00";
            }
            this.label5 .Text = zong.ToString();
            Class1.zong = zong;
        }
        //显示类型的方法
        public void BoinComdoBox()
        {
            string sql = "select * from CommodityClass";
            DataTable dt = DBHelper.SelectForTable(sql);
            DataRow dr = dt.NewRow();
            dr["ClassId"] = 0;
            dr["ClassName"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            this.comboBox1.DisplayMember = "ClassName";
            this.comboBox1.ValueMember = "ClassId";
            this.comboBox1.DataSource = dt;
        }
        public void BoinDataGridView1()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            string sql = "select c.CyId,c.CyName,c.CyPrice from  Commodity c,CommodityClass t where t.ClassId=c.CyClass and 1=1";
            int id = Convert.ToInt32(this.comboBox1.SelectedValue);

            if (id != 0)
            {
                sql += string.Format(" and c.CyClass={0}", id);
            }
            string name = this.textBox1.Text;
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and c.CyName like '%{0}%'", name);
            }
            this.dataGridView1.DataSource = DBHelper.SelectForTable(sql);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BoinDataGridView1();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            BoinDataGridView1();
        }
        
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            TianjiaXfxInfo();
            dataGridView2.AutoGenerateColumns = false;
            string sql = string.Format("select * from Consume c,Commodity y,CetaeInfo o where o.CeId=c.CsoName and y.CyId=c.SsoName and c.CsoRoom=" + id);
            DataTable dt = DBHelper.SelectForTable(sql);
            dataGridView2.DataSource = dt;
        }
        public void DianCanBiao()
        {
            dataGridView2.AutoGenerateColumns = false;
            string sql = string.Format("select * from Consume c,Commodity y where y.CyId=c.SsoName ");
            DataTable dt = DBHelper.SelectForTable(sql);
            dataGridView2.DataSource = dt;
        }
       
        public void TianjiaXfxInfo()

        {
            string spbianhao = "";
            int danjia = 0;

            int str = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
            string sql = "select * from Commodity c,CommodityClass s where c.CyClass=s.ClassId and c.CyId="+str;
            SqlDataReader reader = DBHelper.SelectMore(sql);
            if (reader.Read())
            {
                spbianhao=reader["CyId"].ToString();//商品编号
                danjia = Convert.ToInt32(reader["CyPrice"]);//商品单价
            }
            reader.Close();
            int num = Convert.ToInt32(numericUpDown1.Value);//接收数字框的数字
            string sql1=string.Format("select Count(1) from Consume where CsoRoom={0} and SsoName={1} ",id,spbianhao);
            int chashu = Convert.ToInt32(DBHelper.SelectOne(sql1));
            if (chashu>0)
            {
                string xiugai = string.Format("update Consume set SsooSum+={0},CsPrice+={1}*{2}  where CsoRoom='{3}' and SsoName={4} ", numericUpDown1.Value, danjia, num, id, spbianhao);
                int hang = DBHelper.ExecuteUpdate(xiugai);
                string xiugai1 = string.Format("update TongJioo set SsooSum+={0},CsPrice+={1}*{2}  where CsoRoom='{3}' and SsoName={4} ", numericUpDown1.Value, danjia, num, id, spbianhao);
                int hang1 = DBHelper.ExecuteUpdate(xiugai1);
                if (hang>0 )
                {
                    jiagezong();
                    DianCanBiao();
                }
            }
            else
            {               
                string sqloo = string.Format("select c.Ceid from CetaeInfo c,RoomInfo r where r.RmId=c.CeFangHao and r.RmState=3 and c.CeFangHao={0}", id);
                int name = Convert.ToInt32(DBHelper.SelectOne(sqloo));
                
                string CsoLogin = "Admin";
                DateTime at=DateTime.Now;
                int sss=Convert.ToInt32( danjia * numericUpDown1.Value);
                string sql2 = string.Format("insert into Consume values({0},'{1}',{2},{3},{4},{5},'{6}','{7}')", id, name, spbianhao, num, danjia,sss, at, CsoLogin);
                int hang = DBHelper.ExecuteUpdate(sql2);
                string sql21 = string.Format("insert into TongJioo values({0},'{1}',{2},{3},{4},{5},'{6}','{7}')", id, name, spbianhao, num, danjia, sss, at, CsoLogin);
                int hang1 = DBHelper.ExecuteUpdate(sql21);
                if (hang>0)
                {
                    MessageBox.Show("添加成功！");
                    jiagezong();
                    DianCanBiao();
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            
            }  
        }
        private void button3_Click(object sender, EventArgs e)
        {
            TianjiaXfxInfo();
            dataGridView2.AutoGenerateColumns = false;
            string sql = string.Format("select * from Consume c,Commodity y,CetaeInfo o where o.CeId=c.CsoName and y.CyId=c.SsoName and c.CsoRoom=" + id);
            DataTable dt = DBHelper.SelectForTable(sql);
            dataGridView2.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("入账成功！","提示信息！");
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选中一个商品");
                return;
            }
            int Sub = Convert.ToInt32(dataGridView2.SelectedRows[0].Cells["Column10"].Value);
            FrmShangPinTiudan a = new FrmShangPinTiudan();
            a.Sub = Sub;
            a.fanghao = id;
            DialogResult dr = a.ShowDialog();
            if (dr == DialogResult.OK)
            {
                jiagezong();
                DianCanBiao();
                dataGridView2.AutoGenerateColumns = false;
                string sql = string.Format("select * from Consume c,Commodity y,CetaeInfo o where o.CeId=c.CsoName and y.CyId=c.SsoName and c.CsoRoom=" + id);
                DataTable dt = DBHelper.SelectForTable(sql);
                dataGridView2.DataSource = dt;
            }
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
